<?

namespace Admin\Model;

use Think\Model;

class AdminModel extends Model {

    //判断当前用户是否登录
    /*
     * return true/false
     */
    public function islogin() {
        $admin_id = session('admin.admin_id', ""); //管理员id
        $admin_username = session('admin.username', ""); //管理员username
        $admin_role = session('admin.role', ""); //管理员role
        if (!$admin_id || !$admin_username || !$admin_role) {
            return false;
        } else {
            return true;
        }
    }

    //判断当前用户是否登录
    /*
     * controller 控制器
     * action 方法
     * return true/false
     */
    public function isoperate($controller, $action) {
        if (!$controller || !$action)
            return false;
        //管理员组信息
        $group_id = session('admin.role', ""); //管理员role
        $groupinfo = F('group_menu_' . $group_id); //获取本管理员组的group_menu缓存数组
        //$groupinfo=M("Admingroup")->where("group_id=".$group_id)->field('group_menu')->find();
        //$groupinfo=explode(",",$groupinfo['group_menu']);
        //菜单信息
        $menuinfo = M("Menuinfo")->where("menu_controller='" . strtolower($controller) . "' and menu_action='" . strtolower($action) . "'")->find();
 
        if (in_array($menuinfo['menu_id'], $groupinfo) || !$menuinfo) {//在数组中或者查无结果，均为授权
            return true;
        } else {
            return false;
        }
    }

    //管理员登录
    /*
     * username 用户名
     * password 密码
     * return true/false
     */
    public function adminlogin($username, $password) {
        if (!$username || !$password)
            return false;

        $data = $this->where("username='" . $username . "'")->find();
        if ($data) {
            if ($data['password'] == md5(md5($password) . $username)) {
                session('admin', $data);
                //登录时将本管理员组的group_menu缓存数组更新
                $groupinfo = M("Admingroup")->where("group_id=" . $data['role'])->field('group_id,group_menu')->find();
                F("group_menu_" . $groupinfo['group_id'], explode(",", $groupinfo['group_menu']));
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    //管理员修改密码
    /*
     * admin_id 管理员id
     * oldpassword 原密码
     * newpassword 新密码
     * return true/false
     */
    public function pswadmin($admin_id, $oldpassword, $newpassword) {
        if (!$admin_id || !$oldpassword || !$newpassword)
            return false;

        $data = $this->where("admin_id=" . $admin_id)->find();
        if ($data) {
            if ($data['password'] == md5(md5($oldpassword) . $data['username'])) {
                $newpassword = md5(md5($newpassword) . $data['username']);
                $this->where("admin_id=" . $admin_id)->save(array("password" => $newpassword));
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    //管理员增加
    /*
     * info 管理员增加信息
     * return true/false
     */
    public function addadmin($info) {
        if (!$info)
            return false;

        $info['username'] = trim($info['username']);
        $info['password'] = trim($info['password']);
        $info['role'] = trim($info['role']);
        $info['addtime'] = SYS_TIME;
        $info['password'] = md5(md5($info['password']) . $info['username']);
        $date = $this->add($info);
        return true;
    }

    //管理员编辑
    /*
     * admin_id 管理员id
     * info 管理员编辑信息
     * return true/false
     */
    public function editadmin($admin_id, $info) {
        if (!$info || !$admin_id)
            return false;

        $info['role'] = trim($info['role']);
        $this->where("admin_id=" . $admin_id)->save($info);
        return true;
    }

    //代理员修改密码
    /*
     * admin_id 管理员id
     * oldpassword 原密码
     * newpassword 新密码
     * return true/false
     */
    public function pswagency($admin_id, $oldpassword, $newpassword) {
        if (!$admin_id || !$oldpassword || !$newpassword)
            return false;

        $data = $this->where("admin_id=" . $admin_id)->find();
        if ($data) {
            if ($data['password'] == md5(md5($oldpassword) . $data['username'])) {
                $newpassword = md5(md5($newpassword) . $data['username']);
                $this->where("admin_id=" . $admin_id)->save(array("password" => $newpassword));
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    //代理员编辑
    /*
     * admin_id 管理员id
     * info 管理员编辑信息
     * return true/false
     */
    public function editagency($admin_id, $info) {
        if (!$info || !$admin_id)
            return false;
        $info['money_rate'] = trim($info['money_rate']);
        $this->where("admin_id=" . $admin_id)->save($info);
        return true;
    }

}
